package com.tinem.platform.service.message.config;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import lombok.Data;
import lombok.SneakyThrows;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author fzh
 * @version v1
 * @program: platform
 * @className Ali
 * @description
 * @site
 * @company
 * @create 2021-11-15 21:16
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "ali")
public class AliConfiguration {

	Map<String,Config> config;

	@Data
	public static class Config{

		private IAcsClient iAcsClient;

		String regionId;
		String accessKeyId;
		String secret;
		String endpointName;
		String product;
		String domain;

		public IAcsClient iAcsClient() throws ClientException {
			if(iAcsClient != null){
				return iAcsClient;
			}
			synchronized (this){
				if(iAcsClient != null){
					return iAcsClient;
				}
				IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
				DefaultProfile.addEndpoint(endpointName, regionId, product,  domain);
				this.iAcsClient = new DefaultAcsClient(profile);
				return this.iAcsClient;
			}
		}
	}

}
